*! This file demonstrates how to estimate the double-heteroscedasticity model and the scaling property model proposed in the following papers, respectively.

*! Citation:
*! Wang, H.-J. (2002) ``Heteroscedasticity and Non-Monotonic Efficiency Effects of a Stochastic Frontier Model,'' Journal of Productivity Analysis, 18, pp.241-253.
*! Wang, H.-J. and Schmidt, P. (2002) ``One-Step and Two-Step Estimation of the Effects of Exogenous Variables on Technical Efficiency Levels,'' Journal of Productivity Analysis, 18, pp.129-44.

*! IMPORTANT: If you use Stata interactively, please execute
*! the following two lines, one by one, in the Stata
*! command window in order to avoid further troubles.
*!     set more off
*!     version 10.1
*! You only have to do this once unless you close and reopen Stata.

set trace off
myclear
set more off
set matsize 1000

capture log close
log  using demo_sfmodel_log.txt, text replace


************************************
use milk
********* define explanatory varibles ******

global xvar llabor lfeed lcattle lland

*! ***** OLS *******

reg ly $xvar
mat b_ols = e(b)  /* save the coefficient matrix for later use */


*! ********** double heteroscedasticity model *********************
*! citation:  Wang, H.-J. (2002) ``Heteroscedasticity and Non-Monotonic
*!            Efficiency Effects of a Stochastic Frontier Model,'' Journal
*!            of Productivity Analysis, 18, pp.241-253.

sfmodel ly, prod dist(t) frontier($xvar) mu( comp) usigmas( comp) vsigmas()
sf_init, frontier(b_ols) mu(0 0) usigmas(0 0) vsigmas(0)
sf_srch, n(2) frontier($xvar) mu( comp) usigmas( comp)  fast


ml max, difficult gtol(1e-5) nrtol(1e-5)
sf_transform

sf_predict, bc(bc_t2) jlms(jlms_t2) marginal
label var bc_t2 "efficiency index of double hetero model"
label var jlms_t2 "inefficiency index of double hetero model"


mat b0 = e(b)                  /*  for later use */
mat b_tn1 = b0[1,"frontier:"]
mat b_tn2 = b0[1,"vsigmas:"]


*! ******* scaling property model ************************************************
*! Citation: Wang, H.-J. and Schmidt, P. (2002) ``One-Step and Two-Step
*!           Estimation of the Effects of Exogenous Variables on Technical
*!           Efficiency Levels,'' Journal of Productivity Analysis, 18, pp.129-44.


sfmodel ly, prod dist(t) frontier($xvar)  scaling hscale(comp) tau cu vsigmas()
sf_init, frontier(b_tn1) hscale(0.1) vsigmas(b_tn2) tau(-0.1) cu(-0.1)
sf_srch, n(2) frontier($xvar) hscale(comp) nograph fast
ml max, difficult gtol(1e-5) nrtol(1e-5)
sf_transform

sf_predict, bc(bc_s) jlms(jlms_s) marginal
label var bc_s "efficiency index of scaling property model"
label var jlms_s "inefficiency index of scaling property model"
